<?php


namespace app\api\controller;


use think\Controller;

class Books extends Controller
{
    /**
     * 书籍列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bookList()
    {
        $key = input("apiKey");
        if(empty($key)) return json(['code' => '-10010','msg' => 'APIKEY_NOT_EMPTY']);
        $api = db("api")->where('apikey',$key)->find();
        if(empty($api)) return json(['code' => '-10011','msg' => 'APIKEY_ERROR']);
        $book_list_arr = explode("/",$api['books']);
        $book_list_str = implode(",",$book_list_arr);
        //查询书籍
        $data = db("book")->where('id','in',$book_list_str)->select();
        /*$data = $book_info_list->items();
        $data['total'] = $book_info_list->total();
        $data['per_page'] = 20;
        $data['current_page'] = $book_info_list->currentPage();
        $data['last_page'] = $book_info_list->lastPage();
        */
        foreach($data as $key=>$value){
            unset($data[$key]['recommendation']);
            unset($data[$key]['original']);
            unset($data[$key]['original_new']);
            unset($data[$key]['new']);
            unset($data[$key]['m_id']);
            unset($data[$key]['more_list']);
            unset($data[$key]['new_book_list']);
            unset($data[$key]['ticket_list']);
            unset($data[$key]['tuijian']);
            unset($data[$key]['dasang']);
            unset($data[$key]['auto_free']);
            unset($data[$key]['free_start']);
        }
        return json(['code' => 200,'msg' => 'SUCCESS','data' => $data]);
    }

    /**
     * 章节列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function chapterList(){
        $book_id = input('book_id');
        $key = input("apiKey");
        if(empty($key)) return json(['code' => '-10010','msg' => 'APIKEY_NOT_EMPTY']);
        $api = db("api")->where('apikey',$key)->find();
        if(empty($api)) return json(['code' => '-10011','msg' => 'APIKEY_ERROR']);
        $book_list_arr = explode("/",$api['books']);
        if (!in_array($book_id, $book_list_arr)) return json(['code' => '-10013','msg' => 'BOOK_ID_ERROR']);
//        $chapter_list = db("chapter")->where('book_id',$book_id)->paginate(20);
        $chapter_list = db("chapter")->where('book_id',$book_id)->select();
        return json(['code' => 200,'msg' => 'SUCCESS','data' => $chapter_list]);
    }

    /**
     * 书籍内容
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function chapterContent(){
        $chapter_id = input('chapter_id');
        $key = input("apiKey");
        if(empty($key)) return json(['code' => '-10010','msg' => 'APIKEY_NOT_EMPTY']);
        if(empty($chapter_id)) return json(['code' => '-10010','msg' => 'CHAPTERID_NOT_EMPTY']);
        $api = db("api")->where('apikey',$key)->find();
        if(empty($api)) return json(['code' => '-10011','msg' => 'APIKEY_ERROR']);
        $book_list_arr = explode("/",$api['books']);
        $chapter = db("chapter")->where('id',$chapter_id)->find();
        if (!in_array($chapter['book_id'], $book_list_arr)) return json(['code' => '-10013','msg' => 'BOOK_ID_ERROR']);
        $content = db("content")->where('id',$chapter['content_id'])->find();
        return json(['code' => 200,'msg' => 'SUCCESS','data' => $content]);
    }

    /**
     * 书籍类别
     * @return \think\response\Json
     */
    public function category()
    {
        $key = input("apiKey");
        if(empty($key)) return json(['code' => '-10010','msg' => 'APIKEY_NOT_EMPTY']);
//        $sess_data = session('sess_data');
//        if(!empty($sess_data)) return $sess_data;
        $data = $this->getCateByPid(0);
//        session('sess_data',json(['code' => 200,'msg' => 'SUCCESS','data' => $data]));
        return json(['code' => 200,'msg' => 'SUCCESS','data' => $data]);
    }
    public function getCateByPid($pid)
    {
        $data = db("cate")->where("pid", $pid)->select();
        $res  = [];
        //遍历 递归
        foreach ($data as $key => $value) {
            // 代码段：核心点
            $value['child'] = $this->getcatebypid($value['id']);
            $res[]         = $value;
        }
        return $res;
    }
}